import Vue from "vue";
import VueRouter from "vue-router";

//终极解决多次触发push或者repalce，报错的问题
//NavigationDuplicated

const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function(location,onResolved,onRejected){
  if(onResolved === undefined && onRejected === undefined){
    // originPush.call目的是让VueRouter实例化对象去调用
    //如果不加，那就是window在调用
    return originPush.call(this,location).catch(() => {})
  }else{
    return originPush.call(this,location,onResolved,onRejected)
  }
}

VueRouter.prototype.replace = function(location,onResolved,onRejected){
  if(onResolved === undefined && onRejected === undefined){
    // originPush.call目的是让VueRouter实例化对象去调用
    //如果不加，那就是window在调用
    return originReplace.call(this,location).catch(() => {})
  }else{
    return originReplace.call(this,location,onResolved,onRejected)
  }
}

const home = () => import("../views/home.vue");
const messageboard = () => import("../views/messageboard.vue");
const flie = () => import("../views/flie.vue");
const gundamwall = () => import("../views/gundamwall.vue");
const aboutme = () => import("../views/aboutme.vue");
const project = () => import("../views/project.vue");
const fliecontent = () =>import("../components/flie/fliecontent/fliecontent.vue");
const projectcontent = () =>import("../components/project/projectcontent/projectcontent.vue");
const markdown =()=>import("../views/markDown.vue")

Vue.use(VueRouter);

const routes = [
  {
    path: "/" /* 如果地址为/,代表默认加载,重定向*/,
    redirect: "/home",
    meta: {
      title: "MyBlog-Home",
      Tabs: "",
      index: 1,
    },
  },
  {
    path: "/home",
    component: home,
    name:"home",
    meta: {
      title: "MyBlog-Home",
      Tabs: "首页",
      index: 1,
    },
  },
  {
    path: "/flie",
    component: flie,
    name:"flie",
    meta: {
      title: "MyBlog-Flie",
      Tabs: "文档",
      index: 2,
    },
    children: [
      {
        name: "fliecontent",
        path: "/flie/fliecontent/:sequence",
        component: fliecontent,
      },
    ],
  },
  {
    path: "/project",
    component: project,
    name:"project",
    meta: {
      title: "MyBlog-Project",
      Tabs: "项目",
      index: 3,
    },
    children: [
      {
        name: "projectcontent",
        path: "/project/projectcontent/:sequence",
        component:projectcontent,
      },
    ],
  },
  {
    path: "/gundamwall",
    component: gundamwall,
    name:"gundamwall",
    meta: {
      title: "MyBlog-GundamWall",
      Tabs: "高达墙",
      index: 4,
    },
  },
  {
    path: "/messageboard",
    component: messageboard,
    name:"messageboard",
    meta: {
      title: "MyBlog-MessageBoard",
      Tabs: "留言板",
      index: 5,
    },
  },
  {
    path: "/aboutme",
    component: aboutme,
    name:"aboutme",
    meta: {
      title: "MyBlog-AboutMe",
      Tabs: "关于我",
      index: 6,
    },
  },
  {
    path: "/markdown",
    component: markdown,
    meta: {
      title: "MyBlog-markdown",
      Tabs: "MarkDown",
      index: 7,
    },
  },
];

const router = new VueRouter({
  mode: "hash",
  base: "/system/",
  scrollBehavior: () => ({ y: 0 }),
  routes,
});

router.beforeEach((to, from, next) => {
  document.title=to.matched[0].meta.title;
  next();
});

router.afterEach((to, from, next) => {});

export default router;
